
package com.eltonkent.templates;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.res.XmlResourceParser;
import android.util.Log;

import com.eltonkent.ui.containers.SimpleIconListContainer;
import com.eltonkent.ui.containers.TemplateFieldContainer;

public final class SafeTemplateFactory
{

	/***
	 * Obtains the fields for any given template name
	 * 
	 * @param context
	 * @param parentXml
	 * @param templateName
	 * @return
	 */
	public static ArrayList<TemplateFieldContainer> getFieldContainerList(Context context,
			String templateName)
	{
		ArrayList<TemplateFieldContainer> templateFieldList = new ArrayList<TemplateFieldContainer>();
		try
		{
			XmlResourceParser parser = context.getResources().getXml(com.eltonkent.R.xml.templates);
			parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
			int eventType = parser.getEventType();
			String tagName;

			for(; eventType != XmlPullParser.END_DOCUMENT;)
			{
				if(eventType == XmlPullParser.START_TAG)
				{
					tagName = parser.getName();
					if(tagName.equals(TemplateConstants.SAFE_TEMPLATE_NODE))
					{
						if(parser.getAttributeValue(0).equals(templateName))
						{
							int innerType = parser.nextTag();
							tagName = parser.getName();
							for(; tagName.equals(TemplateConstants.SAFE_ENTRY_NODE);)
							{
								if(innerType == XmlPullParser.START_TAG)
								{
									Log.d("UI", "Current item " + tagName);
									TemplateFieldContainer fieldContainer = new TemplateFieldContainer(
											Integer.parseInt(parser.getAttributeValue(0)), parser
													.getAttributeValue(1), null);
									templateFieldList.add(fieldContainer);
									Log.d("UI", fieldContainer.toString());
								}
								innerType = parser.nextTag();
								tagName = parser.getName();

							}
							return templateFieldList;
						}
					}
				}
				eventType = parser.next();
			}
		} catch(XmlPullParserException e)
		{
			e.printStackTrace();
		} catch(IOException e)
		{
			e.printStackTrace();
		}
		return templateFieldList;
	}

	/**
	 * Obtains the available templates for the given xml resource
	 * 
	 * @param context
	 *            application context
	 * @param xmlResource
	 *            pertaining to the particular folder
	 * @return
	 */
	public static List<SimpleIconListContainer> getTemplateList(Context context, int resource)
	{

		List<SimpleIconListContainer> container = new ArrayList<SimpleIconListContainer>();
		try
		{
			XmlResourceParser parser = context.getResources().getXml(resource);
			int eventType = parser.getEventType();
			String tagName;

			for(; eventType != XmlPullParser.END_DOCUMENT;)
			{
				if(eventType == XmlPullParser.START_TAG)
				{
					tagName = parser.getName();
					if(tagName.equals(TemplateConstants.SAFE_TEMPLATE_NODE))
					{
						SimpleIconListContainer tc = new SimpleIconListContainer(parser
								.getAttributeValue(0), parser.getAttributeValue(1));
						container.add(tc);
						Log.d("UI", tc.toString());
					}
				}
				eventType = parser.next();
			}
		} catch(XmlPullParserException e)
		{
			e.printStackTrace();
		} catch(IOException e)
		{
			e.printStackTrace();
		}
		return container;

	}
}
